<?php

namespace App\Http\Controllers\Home;

use App\Models\Attr;
use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Good;
use App\Http\Controllers\Controller;

class addControllers extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {

        return view('home/index/add');
    }

    public function auth(Request $request)
    {
        $id = $request->get('id');
        $attrid = Category::where('id',$id)->with('attr')->get();
        $title = [];
        foreach ($attrid[0]->attr as $s){
            $title[] = $s;
        }
        $value = [];
        $attr = Attr::where('cid',$id)->with('attrValue')->get();
        foreach ($attr as $s){
            foreach ($s->attrValue as $item){
                $value[] = $item;
            }
        }
        return response()->json(array(
            'title' => $title,
            'value' => $value
        ));
    }

    public function goods(Request $request)
    {
        $sku = $request->input('sku');
        $skus = Good::where('sku',$sku)->get();
        if(count($skus) == 0){
            $gdid = $request->input('gdid');
            $cid = $request->input('cid');
            $gname = $request->input('gname');
            $store_num = $request->input('store_num');
            $price = $request->input('price');
            $discount = $request->input('discount');
            $art_path = $request->input('art_path');
            $description = $request->input('description');
            $status = $request->input('status');
            $goods = new Good;
            $goods->good_dict_id=$gdid;
            $goods->sku=$sku;
            $goods->cid=$cid;
            $goods->gname=$gname;
            $goods->store_num=$store_num;
            $goods->price=$price;
            $goods->discount=$discount;
            $goods->art_path=$art_path;
            $goods->description=$description;
            $goods->status=$status;
            $goods->save();
            return redirect('addgoods')->with('message','添加成功');
        }else{
            return redirect('addgoods')->with('message','sku重复');
        }


    }
}
